<template>
  <!-- 居中 -->
  <el-row type="flex" justify="center">
    <el-col :span="8">
    
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
      <h2 style="text-align: center">用户登录</h2>
      <el-divider></el-divider>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button type="primary" @click="loginUserInfo()"
            >注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
export default {
  data() {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请输入密码"));
      }
      callback();
    };

    return {
      ruleForm: {
        pass: "",
        username: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        username: [{ validator: checkUsername, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const user = {
            username: this.ruleForm.username,
            password: this.ruleForm.pass,
          };
          console.log(user);
          this.axios
            .post(this.$API_URL + "/userManager/myweb/login", user,{
              headers: {
                "Content-Type": "application/json",
                "access-control-allow-origin": "*",
              }
            })
            .then((res) => {
              const state = res.data.state;
              let message;
              let type;
              switch (state) {
                case 200:
                  (message = "登录成功"), (type = "success");
                  break;
                case 400:
                  (message = "登录失败，请输入用户名或密码,"), (type = "error");
                  break;
                case 401:
                  (message = "登录失败,密码错误"), (type = "error");
                  break;
                case 409:
                  (message = "登录失败，用户名错误"), (type = "error");
                default:
                  break;
              }
              this.$message({
                message: message,
                type: type,
                duration: 1000,
              });
              console.log(res.data);
              if (state === 200) {
                this.$router.push("/userManager");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loginUserInfo() {
      this.$router.push("/loginUserInfo");
    }
  },
};
</script>

<style>
* {
  font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif,
    "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB";
}
/* .el-row {
  background-image: url("@/image/1.png");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 50% 0%;
  height: 1000px;
  position: relative;
}
.el-form{
  margin-top: 205px;
  margin-bottom: 10px;
} */
</style>
